package com.jy.adscm.operation.cwb.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jy.adscm.operation.cwb.bean.CwIncomeexpenditure;
import com.jy.adscm.operation.cwb.service.CwIncomeexpenditureService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zf
 * @since 2019-01-07
 */
@RestController
@RequestMapping("adscm/operation/cwIncomeexpenditure")
public class CwIncomeexpenditureController {
    @Autowired
    CwIncomeexpenditureService cwIncomeexpenditureService;
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    SimpleDateFormat sd=new SimpleDateFormat("yyyyMMddHHmmssSSS");
    @RequestMapping("/insert")
    public SimpleCoreResponse insert(CwIncomeexpenditure cw, HttpServletRequest request,String data){
        JSONObject jsonObject = JSON.parseObject(data);
//        System.out.println(jsonObject.getString("jhtjsj"));
//        System.out.println(jsonObject.getTimestamp("jhtjsj"));

        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        String taskCode=sd.format(new Date()).replaceAll("-","").trim().replaceAll(":","").trim();
        cw.setIeid(UUID.randomUUID().toString());
        cw.setIecode(Long.valueOf(taskCode));//根据日期时间生成流水号
        cw.setBzry(token.getUser().getId().toString());
        cw.setJhfbsj(new Date());
        cw.setCwry(jsonObject.getString("czry"));
        cw.setIename(jsonObject.getString("iename"));
        cw.setFzry(jsonObject.getString("fzry"));
        cw.setJhje(jsonObject.getDouble("jhje"));
        if ("".equals(jsonObject.getString("jhyf"))){
            cw.setJhyf(null);
        }else {
            cw.setJhyf(jsonObject.getString("jhyf"));
        }
        if ("".equals(jsonObject.getString("jhqdsj"))){
            cw.setJhqdsj(null);
        }else {
            cw.setJhqdsj(jsonObject.getString("jhqdsj"));
        }

        if ("".equals(jsonObject.get("jhtjsj"))){
            cw.setJhtjsj(null);
        }else {
            cw.setJhtjsj(jsonObject.getString("jhtjsj"));
        }

        if ("".equals(jsonObject.getString("jhwcsj"))){
            cw.setJhwcsj(null);
        }else {
            cw.setJhwcsj(jsonObject.getString("jhwcsj"));
        }


        cw.setObject(jsonObject.getString("objcet"));
        cw.setRwwczt(jsonObject.getInteger("rwwczt"));
        cw.setSzlx(jsonObject.getInteger("szlx"));
        cw.setRwzt(jsonObject.getInteger("rwzt"));
        cw.setCwry(jsonObject.getString("cwry"));
        return  new SimpleCoreResponse("200","新增成功",cwIncomeexpenditureService.insert(cw));
    }
    @RequestMapping("/update")
    public SimpleCoreResponse update(CwIncomeexpenditure cw,HttpServletRequest request,String data){
        JSONObject jsonObject = JSON.parseObject(data);
        cw.setIeid(jsonObject.getString("ieid"));
        cw.setCwry(jsonObject.getString("czry"));
        cw.setIename(jsonObject.getString("iename"));
        cw.setFzry(jsonObject.getString("fzry"));
        cw.setJhje(jsonObject.getDouble("jhje"));
        if ("".equals(jsonObject.getString("jhyf"))){
            cw.setJhyf(null);
        }else {
            cw.setJhyf(jsonObject.getString("jhyf"));
        }
        if ("".equals(jsonObject.getString("jhqdsj"))){
            cw.setJhqdsj(null);
        }else {
            cw.setJhqdsj(jsonObject.getString("jhqdsj"));
        }

        if ("".equals(jsonObject.get("jhtjsj"))){
            cw.setJhtjsj(null);
        }else {
            cw.setJhtjsj(jsonObject.getString("jhtjsj"));
        }

        if ("".equals(jsonObject.getString("jhwcsj"))){
            cw.setJhwcsj(null);
        }else {
            cw.setJhwcsj(jsonObject.getString("jhwcsj"));
        }
        cw.setSjje(jsonObject.getDouble("sjje"));
        if ("".equals(jsonObject.getString("sjsj"))){
            cw.setSjsj(null);
        }else {
            cw.setSjsj(jsonObject.getString("sjsj"));
        }

        cw.setObject(jsonObject.getString("objcet"));
        cw.setRwwczt(jsonObject.getInteger("rwwczt"));
        cw.setSzlx(jsonObject.getInteger("szlx"));
        cw.setRwzt(jsonObject.getInteger("rwzt"));
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        return new SimpleCoreResponse("200","修改成功",cwIncomeexpenditureService.updateById(cw));
    }
    @RequestMapping("/delete")

    public SimpleCoreResponse delete(CwIncomeexpenditure cw,String data){
        JSONObject jsonObject = JSON.parseObject(data);
        cw.setIeid(jsonObject.getString("ieid"));
        return new SimpleCoreResponse("200","删除成功",cwIncomeexpenditureService.deleteById(cw));
    }

    @RequestMapping("/queryByid")
    public  SimpleCoreResponse queryById(String id){
        return new SimpleCoreResponse("200","查询成功",cwIncomeexpenditureService.selectById(id));
    }
    @RequestMapping("/queryByRWZT")
    public  SimpleCoreResponse queryByRWZT(String rwzt,String rwwczt,String szlx,String key){
        return  new SimpleCoreResponse("200","查询成功",cwIncomeexpenditureService.queryByRWZT(rwzt,rwwczt,szlx,key));
    }

@RequestMapping("/queryCwry")
    public SimpleCoreResponse queryCwry(){
        return new SimpleCoreResponse("200","查询财务人员",cwIncomeexpenditureService.queryCwry());
}
}

